﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageAdmin
{
    public partial class PageCatalog : System.Web.UI.Page
    {
        private CDanhMuc _cdm = new CDanhMuc();

        protected void Page_Load(object sender, EventArgs e)
        {
            
            pnThongBao.Visible = false;
            if (!IsPostBack)
            {
                LoadCombobox();
                LoadTreeView();
            }

        }



        private void LoadTreeView()
        {
            tvCategories.Nodes.Clear();
            List<DanhMuc> list = _cdm.GetListCataLog();
            TreeNode root = new TreeNode();
            root.Value = "0";
            root.Text = "Category";
            CreateCategory(list, root, null);
            tvCategories.Nodes.Add(root);
            tvCategories.ExpandDepth = 1;
        }

        private void CreateCategory(List<DanhMuc> list, TreeNode parent, int? parentID)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].IDDanhMucCha == parentID)
                {
                    TreeNode treenode = new TreeNode();
                    treenode.Value = list[i].IDDanhMuc.ToString();
                    treenode.Text = list[i].TenDanhMuc;
                    CreateCategory(list, treenode, list[i].IDDanhMuc);
                    parent.ChildNodes.Add(treenode);
                }
            }

        }

        private void LoadCombobox()
        {
            List<DanhMuc> list = _cdm.GetListCataLog();
            if (list.Count > 0)
            {
                cbSearch.DataSource = list;
                cbSearch.DataTextField = "TenDanhMuc";
                cbSearch.DataValueField = "IDDanhMuc";
                cbSearch.DataBind();
            }

        }

        private void LoadInfoCatalog(int id)
        {

            DanhMuc dm = _cdm.GetByID(id);
            if (dm != null)
            {
                txtName.Text = dm.TenDanhMuc;
                if (dm.IDDanhMucCha != null)
                    cbParent.SelectedValue = dm.IDDanhMucCha.ToString();
                else
                    cbParent.SelectedIndex = 0;
            }
        }

        private void LoadDDLFromCatalogID(int id)
        {
            List<DanhMuc> list = _cdm.GetListCataLogID(id);
            if (list.Count > 0)
            {
                cbParent.DataSource = list;
                cbParent.DataTextField = "TenDanhMuc";
                cbParent.DataValueField = "IDDanhMuc";
                cbParent.DataBind();
            }
        }


        protected void tvCategories_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (tvCategories.SelectedNode != null)
            {
                cbParent.DataSource = null;
                cbParent.DataBind();
                int id = Convert.ToInt32(tvCategories.SelectedNode.Value);
                LoadDDLFromCatalogID(id);
                LoadInfoCatalog(id);
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            pnThongBao.Visible = true;
            if (txtName.Text.Equals("") || tvCategories.SelectedNode == null)
            {
                pnThongBao.CssClass = "InRight error message";
                lbThongBao.Text = "Error update.";
                return;
            }
            DanhMuc item = new DanhMuc();
            item.TinhTrang = true;
            item.IDDanhMuc = int.Parse(tvCategories.SelectedNode.Value);
            item.TenDanhMuc = txtName.Text.Trim();
            if (cbParent.SelectedIndex > 0)
                item.IDDanhMucCha = int.Parse(cbParent.SelectedValue);
            bool kq = _cdm.UpdateCatalog(item);
            if (kq)
            {
                pnThongBao.CssClass = "InRight success message";
                lbThongBao.Text = "Update Category success.";
                txtName.Text = "";
                cbParent.SelectedIndex = 0;
                tvCategories.ExpandAll();
            }
            else
            {
                pnThongBao.CssClass = "InRight error message";
                lbThongBao.Text = "Update Category error.";
            }
            LoadTreeView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            pnThongBao.Visible = true;
            if (txtName.Text.Equals("") || tvCategories.SelectedValue.Equals(""))
            {
                pnThongBao.CssClass = "InRight error message";
                lbThongBao.Text = "Error update.";
                return;
            }
            int id = int.Parse(tvCategories.SelectedValue);
            bool kq = _cdm.DeleteCatalog(id);
            if (kq)
            {
                lbThongBao.CssClass = "success";
                lbThongBao.Text = "Delete Category success.";
                txtName.Text = "";
                cbParent.SelectedIndex = 0;
            }
            else
            {
                lbThongBao.CssClass = "error";
                lbThongBao.Text = "Delete Category error.";
            }
            LoadTreeView();
        }

        protected void btnGo_Click(object sender, EventArgs e)
        {
            string id = cbSearch.SelectedValue.ToString();
            FindTreeNode(id, tvCategories.Nodes);
            
        }

        private void FindTreeNode(string value, TreeNodeCollection nodecollection) {
            
            for (int i = 0; i < nodecollection.Count; i++) {
                if (nodecollection[i].Value.Equals(value))
                {
                    nodecollection[i].Selected = true;
                    int id = Convert.ToInt32(nodecollection[i].Value);
                    LoadDDLFromCatalogID(id);
                    txtName.Text = nodecollection[i].Text;
                    if (nodecollection[i].Value.Equals(""))
                    {
                        cbParent.SelectedIndex = 0;
                    }
                    else {
                        DanhMuc dm = _cdm.GetByID(id);
                        cbParent.SelectedValue = dm.IDDanhMucCha.ToString();
                    }
                    nodecollection[i].Expand();
                    break;
                }
                FindTreeNode(value, nodecollection[i].ChildNodes);
            }
           
        }
    }
}